/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bingo.megabingo;

import Bingo.forms.Juego;
import java.sql.ResultSet;
import java.sql.SQLException;
import Bingo.system.Exc;
import Bingo.system.Label;
import Bingo.system.Persistencia;
import java.awt.Dimension;
import java.io.InputStream;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author Gilberto
 */
public class Reporte {

    private Persistencia persistencia = new Persistencia();
    private ResultSet rsts;
    private String sql, TableName;
    private Label label = new Label();

    public Reporte() {
        TableName = "reportes.acta_encabezado";
    }

    public void report() throws Exception {
        try {
            DropTable();
            CreateTable();
            LoadData();

            persistencia.dbConnect();

            showReport();

        } catch (Exc ex) {
            String e = ex.toString();
        } catch (SQLException ex) {
            String e = ex.toString();
        } catch (Exception ex) {
            String e = ex.toString();
        } finally {
            persistencia.dbDisconnect();
        }
    }

    private void LoadData() {
        try {
            int columna = (Juego.combo_fila.getSelectedIndex() + 1);
            persistencia.dbConnect();
            //Cargamos en la base de datos los cartones ganadores
            sql = "SELECT id_carton FROM public.cartones_ganadores WHERE numero_bingo = " + columna;
            rsts = persistencia.executeRsts(sql);

            while (rsts.next()) {
                sql = "INSERT INTO " + TableName
                        + " (title, id_carton, detalle) "
                        + "VALUES ('" + label.name("Bingo" + columna) + "', "
                        + rsts.getInt("id_carton") + ", "
                        + "'" + label.name("Detalle") + "')";
                persistencia.executeUpdate(sql);
            }
            rsts.close();
            persistencia.dbDisconnect();
        } catch (Exc ex) {
            String e = ex.toString();
        } catch (SQLException ex) {
            String e = ex.toString();
        } catch (Exception ex) {
            String e = ex.toString();
        }
    }

    private void DropTable() throws Exc, SQLException, Exception {
        persistencia.dbConnect();
        sql = "DROP TABLE IF EXISTS " + TableName;
        persistencia.executeUpdate(sql);
        persistencia.dbDisconnect();
    }

    private void CreateTable() throws Exc, SQLException, Exception {
        persistencia.dbConnect();
        sql = "CREATE TABLE " + TableName
                + " (title text, "
                + "id_carton integer, "
                + "detalle text) "
                + "WITH (OIDS=FALSE); "
                + "ALTER TABLE " + TableName
                + " OWNER TO core;";

        persistencia.executeUpdate(sql);
        persistencia.dbDisconnect();
    }

    public void showReport() {
        //Path to your .jasper file in your package
        String reportName = "Bingo/megabingo/acta.jasper";

        //Get a stream to read the file
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);

        try {
            //Fill the report with parameter, connection and the stream reader     
            JasperPrint jp = JasperFillManager.fillReport(is, null, persistencia.getConn());

            //Viewer for JasperReport
            JRViewer jv = new JRViewer(jp);

            //Insert viewer to a JFrame to make it showable
            JFrame jf = new JFrame();
            jf.getContentPane().add(jv);
            jf.validate();
            jf.setVisible(true);
            jf.setSize(new Dimension(800, 600));
            jf.setLocation(300, 100);
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }
}
